package com.one.common.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.Toast;

import com.one.common.CommonApp;


public class Toaster {

	private static Context sContext;

	private static MyToast sToast;

	private static ToastHandler sToastHandler = new ToastHandler();

	public static final int TOAST_DURATION_SHORT = Toast.LENGTH_SHORT;

	public static final int TOAST_DURATION_LONG = Toast.LENGTH_LONG;

	public static void init() {
		if(sToast == null){
			sContext = CommonApp.getInstance();
			sToast = new MyToast(sContext);
		}

	}

	private static class ToastHandler extends Handler {

		private static final int DELAYED_TOAST = 100;

		private static final int DELAYED_TIME_MILLS = 150;

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == DELAYED_TOAST) {
				showSuperToast((CharSequence) msg.obj, msg.arg1, msg.arg2);
			}
		}

		public void showDelayedSuperToast(CharSequence text, int duration, int type) {
			Message msg = obtainMessage();
			msg.what = DELAYED_TOAST;
			msg.obj = text;
			msg.arg1 = duration;
			msg.arg2 = type;
			sendMessageDelayed(msg, DELAYED_TIME_MILLS);
		}
	}

	/**
	 * 显示Toast的地方
	 * @param text
	 * @param duration
	 */
	public static void showSuperToast(CharSequence text, int duration, int type) {

		if(sToast == null) {
			sToast = new MyToast(sContext);
		}
		sToast.setGravity(Gravity.CENTER,0,0);//中间显示
		sToast.setText(text);
		sToast.setDuration(duration);
		sToast.setType(type);
		sToast.show();


	}

	public static void showToast(CharSequence text, int duration, boolean immediateShow, int type) {
		if (immediateShow) {
			showSuperToast(text, duration,type);
		} else {
			sToastHandler.showDelayedSuperToast(text, duration,type);
		}
	}

	/**
	 * default to show short toast delayed
	 *
	 * @param text
	 */
	public static void showShortToast(CharSequence text) {
		showShortToast(text, false, -1);
	}

	public static void showShortToast(CharSequence text, int type) {
		showShortToast(text, false, type);
	}

	/**
	 * default to show short toast delayed
	 *
	 * @param resId
	 */
	public static void showShortToast(int resId) {
		showShortToast(sContext.getResources().getString(resId));
	}

	public static void showShortToast(int resId, int type) {
		showShortToast(sContext.getResources().getString(resId), type);
	}

	/**
	 * default to show long toast delayed
	 *
	 * @param text
	 */
	public static void showLongToast(CharSequence text) {
		showLongToast(text, false, -1);
	}

	public static void showLongToast(CharSequence text, int type) {
		showLongToast(text, false, type);
	}


	/**
	 * default to show long toast delayed
	 *
	 * @param resId
	 */
	public static void showLongToast(int resId) {
		showLongToast(sContext.getResources().getString(resId));
	}

	public static void showLongToast(int resId, int type) {
		showLongToast(sContext.getResources().getString(resId),type);
	}

	/**
	 * choose whether to show the short toast delayed
	 *
	 * @param text
	 */
	public static void showShortToast(CharSequence text, boolean immediateShow, int type) {
		showToast(text, TOAST_DURATION_SHORT, immediateShow, type);
	}

	/**
	 * choose whether to show the short toast delayed
	 *
	 * @param resId
	 */
	public static void showShortToast(int resId, boolean immediateShow, int type) {
		showShortToast(sContext.getResources().getString(resId), immediateShow, type);
	}

	/**
	 * choose whether to show the long toast delayed
	 *
	 * @param text
	 */
	public static void showLongToast(CharSequence text, boolean immediateShow, int type) {
		showToast(text, TOAST_DURATION_LONG, immediateShow,type);
	}

	/**
	 * choose whether to show the long toast delayed
	 *
	 * @param resId
	 */
	public static void showLongToast(int resId, boolean immediateShow, int type) {
		showLongToast(sContext.getResources().getString(resId), immediateShow,type);
	}


}
